package com.draw.server.gui.panel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;

import com.draw.core.Color;
import com.draw.server.bdd_manager.ColorManager;
import com.draw.server.gui.model.DataTableModel;

/**
 * Cette classe est le panel (de l'interface graphique du serveur)
 * qui permet à l'administrateur du serveur d'ajouter des couleurs en BDD sur le serveur.
 * @author Damien Tesniere
 *
 */
public class ColorPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 3578365842799697676L;

	private static ColorPanel instance = new ColorPanel();
	private JButton save;
	//private JButton cancel;
	private JTable colorTable;
	private DataTableModel dataTableModel;
	
	private String[] columnNames = {"Red",
									"Green", "Blue", "Affichage"};
	
	private JColorChooser jcc;

	public static ColorPanel getInstance() {
		return instance;
	}
	
	public Object[][] getColorForTable() {	
		
		List<Color> colorList = ColorManager.getAllColor();
		
		Object[][] data = new Object[colorList.size()][4];

		int i=0;
		for(Color color : colorList) {
			data[i][0] = color.getRed();
			data[i][1] = color.getGreen();
			data[i][2] = color.getBlue();
			data[i][3] = color.getColor();
			i++;
		}

		return data;
	}
	
	public ColorPanel() {
		Object[][] data = this.getColorForTable();
		
		this.setLayout(new BorderLayout());
		
		JPanel pan = new JPanel(new FlowLayout());
		this.add(new JLabel("Couleur"));
		
		//Ajout des boutnons

		this.save = new JButton("Sauvegarder");
		//this.cancel = new JButton("Annuler");
		
		this.save.addActionListener(this);
		//this.cancel.addActionListener(this);
		
		pan.add(this.save);
		//pan.add(this.cancel);
		
		this.dataTableModel = new DataTableModel();
		dataTableModel.setHeader(this.columnNames);
		dataTableModel.setData(data);
		

		this.colorTable = new JTable(dataTableModel);
		this.colorTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);		
		this.colorTable.setFillsViewportHeight(true);
		
		
		TableColumn col = colorTable.getColumnModel().getColumn(3);
		col.setCellRenderer(new AffichageCouleurDansCellule());
	//	this.colorTable.getSelectionModel().addListSelectionListener(new SelectionChange(this.colorTable, this.wordField, this.difficultyField));
		
		jcc = new JColorChooser();
		jcc.setPreviewPanel(new JPanel());
		
        //Ajout en bas du selecteur de couleur et du bouton de sauvegarde
		JPanel panel_bas = new JPanel(new BorderLayout());
		panel_bas.add(jcc, BorderLayout.NORTH);
		panel_bas.add(this.save, BorderLayout.SOUTH);
        add(panel_bas, BorderLayout.PAGE_END);
		
		JScrollPane scrollPane = new JScrollPane(this.colorTable);
		this.add(scrollPane);
		
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(pan,BorderLayout.SOUTH);
		
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if ( e.getSource().equals(this.save) ) {
		//JOptionPane.showMessageDialog(null, this.jcc.getColor().toString());
			Color c = new Color(jcc.getColor().getRed(), jcc.getColor().getGreen(), jcc.getColor().getBlue());
			ColorManager.addColor(c);
			this.dataTableModel.setData(this.getColorForTable());
		}
	}
}
